package org.lds.ers.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.lds.ers.client.LoginService;
import org.lds.ers.client.model.LoginInfo;
import org.lds.ers.client.model.StakeInfo;
import org.lds.ers.client.model.SysAdminInfo;
import org.lds.ers.server.model.PMF;
import org.lds.ers.server.model.Stake;
import org.lds.ers.server.model.StakeERS;
import org.lds.ers.server.model.SysAdmin;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class LoginServiceImpl extends RemoteServiceServlet implements
		LoginService {

	public LoginInfo login(String requestUri) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		LoginInfo loginInfo = new LoginInfo();
		
		if (user != null) {
			loginInfo.setLoggedIn(true);
			loginInfo.setEmailAddress(user.getEmail());
			loginInfo.setNickname(user.getNickname());
			loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));
			loginInfo.setIsAdmin(isAdmin(user.getEmail())); 
		} else {
			loginInfo.setLoggedIn(false);
			loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
		}
		
		return loginInfo;
	}
	
	private Boolean isAdmin(String userID)
	{
		Boolean result = userID.toUpperCase().equals("KEVIN.HUANG@LDSHC.ORG") ;
		if (!result) 
		{
			PersistenceManager pm = PMF.get().getPersistenceManager();
		    //String query = "select from " + StakeERS.class.getName() + " order by userID asc";
			Query query = pm.newQuery(SysAdmin.class, "userID == theUserID ");
			query.declareParameters("String theUserID");

		    List<StakeERS> ss = (List<StakeERS>) query.execute(userID);
		    result = (ss.size() >0 );
		}
		
	    return (result);
	}

	public SysAdminInfo addAdmin(SysAdminInfo info) {
		
		SysAdminInfo result = null;
		SysAdmin s = new SysAdmin(info.getUserID(), info.getRemark(), new Date());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			pm.makePersistent(s);
			info.setID(s.getId());
			result = info;
		}
		catch (Exception ex) {
			result = null;
		}
		finally {
			pm.close();
		}
		
		return result ;
	}

	public Boolean removeAdmin(Long id) {
		Boolean result = false;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			SysAdmin s = pm.getObjectById(SysAdmin.class, id);

			pm.deletePersistent(s);
			result = true;
		}
		catch (Exception ex) {
			result = false;
		}
		finally {
			pm.close();
		}
		return result ;
	}

	public List<SysAdminInfo> getAdmins() {
		List<SysAdminInfo> admins = new ArrayList<SysAdminInfo>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + SysAdmin.class.getName() + " order by userID asc";
	    List<SysAdmin> ss = (List<SysAdmin>) pm.newQuery(query).execute();
	    for (SysAdmin s : ss) {
	    	SysAdminInfo info = new SysAdminInfo();
	    	info.setID(s.getId());
	    	info.setUserID(s.getUserID());
	    	info.setRemark(s.getRemark());
	    	info.setLastUpdate(s.getLastUpdate());
	    	
	    	admins.add(info);
	    }
	    
		return admins;
	}
}
